﻿using ClientInfrastructure.Model;
using ShareCode.Dto;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing.Printing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Wpf.Ui.Controls;

namespace ClientAppraise.Controls;
public class ZxPagination : Control
{

    public static readonly DependencyProperty PagingModelProperty;
    public static readonly DependencyProperty PageSizeProperty;
    public static readonly DependencyProperty ListPageSizeProperty;
    public static readonly DependencyProperty ListPageShowProperty;
    public static readonly DependencyProperty ShowTitleProperty;

    //public static readonly RoutedEvent PaneClosedEvent;

    static ZxPagination()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ZxPagination), new FrameworkPropertyMetadata(typeof(ZxPagination)));

        PagingModelProperty = DependencyProperty.Register(nameof(PagingModel), typeof(ClientPagingModel), typeof(ZxPagination), new PropertyMetadata(null, OnPagingModelCallback));
        PageSizeProperty = DependencyProperty.Register(nameof(PageSize), typeof(int), typeof(ZxPagination), new PropertyMetadata(0, OnPageSizeChanged));
        ListPageSizeProperty = DependencyProperty.Register(nameof(ListPageSize), typeof(Dictionary<string, int>), typeof(ZxPagination), new PropertyMetadata(new Dictionary<string, int>() { { "5 条/页", 5 }, { "10 条/页", 10 }, { "20 条/页", 20 }, { "50 条/页", 50 }, { "100 条/页", 100 } }));
        ListPageShowProperty = DependencyProperty.Register(nameof(ListPageShow), typeof(ObservableCollection<ListPageShowModel>), typeof(ZxPagination));
        ShowTitleProperty = DependencyProperty.Register(nameof(ShowTitle), typeof(string), typeof(ZxPagination));


        //PaneClosedEvent = EventManager.RegisterRoutedEvent(nameof(PaneClosed), RoutingStrategy.Bubble, typeof(TypedEventHandler<ZxNavigationView, RoutedEventArgs>), typeof(ZxNavigationView));


    }



    [Bindable(true)]
    public ClientPagingModel PagingModel
    {
        get => (ClientPagingModel)GetValue(PagingModelProperty);
        set => SetValue(PagingModelProperty, value);
    }

    public int CurrentPage
    {
        get;
        set;
    }
    public int PageSize
    {
        get => (int)GetValue(PageSizeProperty);
        set => SetValue(PageSizeProperty, value);
    }
    public int TotalRows
    {
        get;
        set;
    }
    public int MaxPage => (int)float.Ceiling(TotalRows / PageSize)+1;
    public string ShowTitle
    {
        get => (string)GetValue(ShowTitleProperty);
        set => SetValue(ShowTitleProperty, value);
    }

    public Dictionary<string, int> ListPageSize
    {
        get => (Dictionary<string, int>)GetValue(ListPageSizeProperty);
        set => SetValue(ListPageSizeProperty, value);
    }
    public ObservableCollection<ListPageShowModel> ListPageShow
    {
        get => (ObservableCollection<ListPageShowModel>)GetValue(ListPageShowProperty);
        set => SetValue(ListPageShowProperty, value);
    }


    //public event TypedEventHandler<ZxNavigationView, RoutedEventArgs> PaneClosed
    //{
    //    add => AddHandler(PaneClosedEvent, value);
    //    remove => RemoveHandler(PaneClosedEvent, value);
    //}


    private void PagingModelPageCallback(PagingDto newValue)
    {
        if (newValue == default)
        {
            return;
        }

        if (ListPageShow == default)
        {
            ListPageShow = new ObservableCollection<ListPageShowModel>();
        }

        ListPageShow.Clear();

        CurrentPage = newValue.CurrentPage;
        PageSize = newValue.PageSize;
        TotalRows = newValue.TotalRows;

        ListPageShow.Add(new ListPageShowModel("<", CurrentPage - 5, CurrentPage, MaxPage, this));

        for (var i = 5; i >= -5; i--)
        {
            var temp = CurrentPage - i;
            if (temp > 0 && temp <= MaxPage)
            {
                ListPageShow.Add(new ListPageShowModel(temp.ToString(), temp, CurrentPage, MaxPage, this));
            }
        }

        ListPageShow.Add(new ListPageShowModel(">", CurrentPage + 5, CurrentPage, MaxPage, this));


        ShowTitle = $"第 {((CurrentPage - 1) * PageSize)+1}-{CurrentPage * PageSize} 条/总共 {TotalRows} 条";
    }
    private static void OnPagingModelCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not ZxPagination control)
        {
            return;
        }

        control.PagingModelPageCallback((PagingDto)e.NewValue);
    }

    private static void OnPageSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not ZxPagination control)
        {
            return;
        }

        control.PagingModel.PageSize = (int)e.NewValue;
        // control.PageSize = control.PagingModel.PageSize;

        if (control.PagingModel.CurrentPage > control.MaxPage)
        {
            control.PagingModel.CurrentPage = control.MaxPage;
        }


        control.PagingModel.DoSearch?.Invoke();
    }

    public void Button_Click(ListPageShowModel btn)
    {

        if (btn == default)
        {
            return;
        }

        if (btn.PageIndex == default)
        {
            return;
        }


        if (btn.PageIndex == CurrentPage)
        {
            return;
        }

        PagingModel.CurrentPage = btn.PageIndex;

        if (PagingModel.CurrentPage > MaxPage)
        {
            PagingModel.CurrentPage = MaxPage;
        }
        else if (PagingModel.CurrentPage < 1)
        {
            PagingModel.CurrentPage = 1;
        }

        PagingModel.DoSearch?.Invoke();
    }

    private void TheCbb_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var theCbb = (ComboBox)sender;

        PageSize = Convert.ToInt32(theCbb.SelectedValue.ToString());
    }

    public override void OnApplyTemplate()
    {
        if (GetTemplateChild("TheCbb") is ComboBox theCbb)
        {
            theCbb.SelectionChanged -= TheCbb_SelectionChanged;
            theCbb.SelectionChanged += TheCbb_SelectionChanged;
        }
    }

    


}
